from random import randrange

from flask.json import jsonify
from flask import Flask, render_template

from pyecharts import options as opts
from pyecharts.charts import Line, Bar

from ryu_rest_api import get_switch_countinfo


app = Flask(__name__, static_folder="templates")

def line_base(title) -> Line:
    line = (
        Line()
        .add_xaxis([])
        .add_yaxis(series_name="s1", y_axis=[], is_smooth=True, label_opts=opts.LabelOpts(is_show=True),)
        .add_yaxis(series_name="s2", y_axis=[], is_smooth=True, label_opts=opts.LabelOpts(is_show=True),)
        .add_yaxis(series_name="s3", y_axis=[], is_smooth=True, label_opts=opts.LabelOpts(is_show=True),)
        .add_yaxis(series_name="s4", y_axis=[], is_smooth=True, label_opts=opts.LabelOpts(is_show=True),)
        .add_yaxis(series_name="s5", y_axis=[], is_smooth=True, label_opts=opts.LabelOpts(is_show=True),)
        .set_series_opts(
            areastyle_opts=opts.AreaStyleOpts(opacity=0.3),
            label_opts=opts.LabelOpts(is_show=False)
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title=title),
            xaxis_opts=opts.AxisOpts(type_="value"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            datazoom_opts=opts.DataZoomOpts(),
        )
    )
    return line

def bar_base(title, data) -> Bar:
    c = (
        Bar()
        .add_xaxis(['s1', 's2', 's3', 's4', 's5'])
        .add_yaxis(title, data)
        .set_series_opts(
            areastyle_opts=opts.AreaStyleOpts(opacity=0.3),
            label_opts=opts.LabelOpts(is_show=False)
        )
        .set_global_opts(title_opts=opts.TitleOpts(title=title))
    )
    return c



# @app.route("/test/<int:dpid>")
# def test(dpid):
#     return '{"%d": [{"packet_count": 146, "byte_count": 13148, "flow_count": 1}]}' % (dpid)

@app.route("/")
def index():
    return render_template("index.html")


@app.route("/lineChart")
def get_line_chart1():
    c = line_base(title="包数每秒")
    return c.dump_options_with_quotes()

@app.route("/byteChart")
def get_line_chart2():
    c = line_base(title="字节每秒")
    return c.dump_options_with_quotes()


@app.route("/pkt_barChart")
def get_pkt_bar_chart():
    s1_pkt_cnt, _, _ = get_switch_countinfo(1)
    s2_pkt_cnt, _, _ = get_switch_countinfo(2)
    s3_pkt_cnt, _, _ = get_switch_countinfo(3)
    s4_pkt_cnt, _, _ = get_switch_countinfo(4)
    s5_pkt_cnt, _, _ = get_switch_countinfo(5)

    # test
    # s1_pkt_cnt, s2_pkt_cnt, s3_pkt_cnt, s4_pkt_cnt, s5_pkt_cnt = (randrange(0, 100), randrange(0, 100), randrange(0, 100), randrange(0, 100), randrange(0, 100))

    pkt_bar = bar_base(title='包数统计', data=[s1_pkt_cnt, s2_pkt_cnt, s3_pkt_cnt, s4_pkt_cnt, s5_pkt_cnt])
    return pkt_bar.dump_options_with_quotes()

@app.route("/byte_barChart")
def get_byte_bar_chart():
    _, s1_byte_cnt, _ = get_switch_countinfo(1)
    _, s2_byte_cnt, _ = get_switch_countinfo(2)
    _, s3_byte_cnt, _ = get_switch_countinfo(3)
    _, s4_byte_cnt, _ = get_switch_countinfo(4)
    _, s5_byte_cnt, _ = get_switch_countinfo(5)

    # test
    # s1_byte_cnt, s2_byte_cnt, s3_byte_cnt, s4_byte_cnt, s5_byte_cnt = (randrange(0, 100), randrange(0, 100), randrange(0, 100), randrange(0, 100), randrange(0, 100))

    byte_bar = bar_base(title='字节统计', data=[s1_byte_cnt, s2_byte_cnt, s3_byte_cnt, s4_byte_cnt, s5_byte_cnt])
    return byte_bar.dump_options_with_quotes()

@app.route("/flow_barChart")
def get_flow_bar_chart():
    _, _, s1_flow_cnt = get_switch_countinfo(1)
    _, _, s2_flow_cnt = get_switch_countinfo(2)
    _, _, s3_flow_cnt = get_switch_countinfo(3)
    _, _, s4_flow_cnt = get_switch_countinfo(4)
    _, _, s5_flow_cnt = get_switch_countinfo(5)

    # test
    # s1_flow_cnt ,s2_flow_cnt, s3_flow_cnt, s4_flow_cnt, s5_flow_cnt = (randrange(0, 100), randrange(0, 100), randrange(0, 100), randrange(0, 100), randrange(0, 100))

    flow_bar = bar_base(title='流表统计', data=[s1_flow_cnt, s2_flow_cnt, s3_flow_cnt, s4_flow_cnt, s5_flow_cnt])
    return flow_bar.dump_options_with_quotes()


now_count = -1    # 从开始到现在的采集次数

s1_last_packet_count = 0    # 交换机s1上次采集时包数
s2_last_packet_count = 0    # 交换机s2上次采集时包数
s3_last_packet_count = 0    # 交换机s3上次采集时包数
s4_last_packet_count = 0    # 交换机s4上次采集时包数
s5_last_packet_count = 0    # 交换机s5上次采集时包数

s1_last_byte_count = 0    # s1上次采集时的字节数
s2_last_byte_count = 0    # s2上次采集时的字节数
s3_last_byte_count = 0    # s3上次采集时的字节数
s4_last_byte_count = 0    # s4上次采集时的字节数
s5_last_byte_count = 0    # s5上次采集时的字节数

@app.route("/lineDynamicData")
def update_line_data():
    global now_count
    global s1_last_packet_count, s2_last_packet_count
    global s3_last_packet_count, s4_last_packet_count, s5_last_packet_count
    global s1_last_byte_count, s2_last_byte_count
    global s3_last_byte_count, s4_last_byte_count, s5_last_byte_count

    now_count += 1
    s1_packet_count, s1_byte_count, _ = get_switch_countinfo(1)
    s2_packet_count, s2_byte_count, _ = get_switch_countinfo(2)
    s3_packet_count, s3_byte_count, _ = get_switch_countinfo(3)
    s4_packet_count, s4_byte_count, _ = get_switch_countinfo(4)
    s5_packet_count, s5_byte_count, _ = get_switch_countinfo(5)

    # test
    # s1_packet_count, s1_byte_count, _ = (randrange(0, 100), randrange(0, 100), randrange(0, 100))
    # s2_packet_count, s2_byte_count, _ = (randrange(0, 100), randrange(0, 100), randrange(0, 100))
    # s3_packet_count, s3_byte_count, _ = (randrange(0, 100), randrange(0, 100), randrange(0, 100))
    # s4_packet_count, s4_byte_count, _ = (randrange(0, 100), randrange(0, 100), randrange(0, 100))
    # s5_packet_count, s5_byte_count, _ = (randrange(0, 100), randrange(0, 100), randrange(0, 100))

    s1_PPS = s1_packet_count - s1_last_packet_count    # s1的每秒包数
    s2_PPS = s2_packet_count - s2_last_packet_count    # s2的每秒包数
    s3_PPS = s3_packet_count - s3_last_packet_count    # s3的每秒包数
    s4_PPS = s4_packet_count - s4_last_packet_count    # s4的每秒包数
    s5_PPS = s5_packet_count - s5_last_packet_count    # s5的每秒包数

    s1_BPS = s1_byte_count - s1_last_byte_count  # s1的每秒字节数
    s2_BPS = s2_byte_count - s2_last_byte_count  # s2的每秒字节数
    s3_BPS = s3_byte_count - s3_last_byte_count  # s3的每秒字节数
    s4_BPS = s4_byte_count - s4_last_byte_count  # s4的每秒字节数
    s5_BPS = s5_byte_count - s5_last_byte_count  # s5的每秒字节数

    s1_last_packet_count = s1_packet_count
    s2_last_packet_count = s2_packet_count
    s3_last_packet_count = s3_packet_count
    s4_last_packet_count = s4_packet_count
    s5_last_packet_count = s5_packet_count

    s1_last_byte_count = s1_byte_count
    s2_last_byte_count = s2_byte_count
    s3_last_byte_count = s3_byte_count
    s4_last_byte_count = s4_byte_count
    s5_last_byte_count = s5_byte_count

    return jsonify({"name1": now_count, "value1": s1_PPS,
                    "name2": now_count, "value2": s2_PPS,
                    "name3": now_count, "value3": s3_PPS,
                    "name4": now_count, "value4": s4_PPS,
                    "name5": now_count, "value5": s5_PPS,
                    "name6": now_count, "value6": s1_BPS,
                    "name7": now_count, "value7": s2_BPS,
                    "name8": now_count, "value8": s3_BPS,
                    "name9": now_count, "value9": s4_BPS,
                    "name10": now_count, "value10": s5_BPS
                   })


if __name__ == "__main__":
    app.run()